#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bof2.h"


int main(void)
{
	FILE *rom;
	FILE *out;
	int buffer;
	int match;
	char mte[MAX];
	char mte_size;
	char unknown[10];
	int x, y,z;
	
		
	if(!(rom = fopen(ROM_NAME, "rb")))
		exit(printf("ERRO ao abrir a ROM %s.\n", ROM_NAME));
	if(!(out = fopen(OUT_NAME, "wb")))
		exit(printf("ERRO ao criar arquivo %s.\n", OUT_NAME));
	
	ReadStaticTbl("BoF2-Static.tbl");
			
	fseek(rom, MTE_START, SEEK_SET);

	for(x = 0; x <= 0xFF; x++)
	{
		mte_size = getc(rom);
		//printf("Tamanho: %02X\n", mte_size);
		for(y = 0; y < mte_size; y++)
		{
			match = 0;
			buffer = getc(rom);
			for(z = 0; z <= TBL_SIZE; z++)
			{
				if( g_static_tbl_hex[z] == buffer )
				{
					mte[y] = g_static_tbl_char[z];
					match = 1;
					break;
				}
			}
			if(match != 1)
			{
				sprintf(unknown,"[$%02X]", y);
				strcat(mte, unknown);
			}
		}
		
		mte[y] = '\0';
		fprintf(out, "03%02X=%s\n", x, mte);
	}
	
	
	fclose(rom);
	fclose(out);
	return 0;
} 
